﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;

namespace nhakhoachauau
{
    public partial class WebForm11 : System.Web.UI.Page
    {
        public decimal TongChi;
        protected void Page_Load(object sender, EventArgs e)
        {
            TongChi = 0;
            if (!IsPostBack)
            {
                dp1.Value = DateTime.Now.Date.ToString("MM-dd-yyyy");
                div_ChonNgay.Visible = true;
                div_ChonThang.Visible = false;
                div_ChonNam.Visible = false;
                lblTitle.Text = "Ngày " + dp1.Value;
            }
        }

        protected void ddlChonNam_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = DateTime.Now.Year; i >= 2000; i--)
                {
                    ddlChonNam.Items.Add(new ListItem(i.ToString(),i.ToString()));
                }
            }
        }

        protected void ddlThongKe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (int.Parse(ddlThongKe.SelectedValue) == (int)enumThongKe.TheoNgay)
            {
                div_ChonNgay.Visible = true;
                div_ChonThang.Visible = false;
                div_ChonNam.Visible = false;
                dp1.Value = DateTime.Now.Date.ToString("MM-dd-yyyy");

                rptThongKeTheoNgay.Visible = true;
                rptThongKeTheoThang.Visible = false;
                rptThongKeTheoNam.Visible = false;

                //lblTitle.Text = "Ngày " + dp1.Value;
            }
            else if (int.Parse(ddlThongKe.SelectedValue) == (int)enumThongKe.TheoThang)
            {
                div_ChonNgay.Visible = false;
                div_ChonThang.Visible = true;
                div_ChonNam.Visible = false;
                txtChonThang.Value = DateTime.Now.Date.ToString("MM-yyyy");

                rptThongKeTheoNgay.Visible = false;
                rptThongKeTheoThang.Visible = true;
                rptThongKeTheoNam.Visible = false;

                //lblTitle.Text = "Tháng " + txtChonThang.Value;
            }
            else if (int.Parse(ddlThongKe.SelectedValue) == (int)enumThongKe.TheoNam)
            {
                div_ChonNgay.Visible = false;
                div_ChonThang.Visible = false;
                div_ChonNam.Visible = true;

                rptThongKeTheoNgay.Visible = false;
                rptThongKeTheoThang.Visible = false;
                rptThongKeTheoNam.Visible = true;

                //lblTitle.Text = "Năm " + ddlChonNam.Text;
            }
            btnThongKe_Click(sender, e);
        }

        protected void rptThongKeTheoNgay_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Q_BUSChi BUSchi = new Q_BUSChi();
                string[] arrValue = dp1.Value.Split('-');
                rptThongKeTheoNgay.DataSource = BUSchi.DanhSachChiTheoNgay(new DateTime(int.Parse(arrValue[2]), int.Parse(arrValue[0]), int.Parse(arrValue[1])));
                rptThongKeTheoNgay.DataBind();
            }
        }

        protected void rptThongKeTheoNgay_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Chi chi = (Chi)e.Item.DataItem;
                Label lblSTT = (Label)e.Item.FindControl("lblSTT");
                Label lblTenNhanVien = (Label)e.Item.FindControl("lblTenNhanVien");
                Q_BUSNhanVien BUSnv = new Q_BUSNhanVien();
                lblTenNhanVien.Text = BUSnv.ThongTinNhanVienTheoID(chi.id_nhanvien).ho_ten;
                TongChi += (decimal)chi.so_tien_chi;
                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
            }
        }

        protected void rptThongKeTheoThang_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ChiTheoThang chi = (ChiTheoThang)e.Item.DataItem;
                Label lblSTT = (Label)e.Item.FindControl("lblSTT");
                TongChi += (decimal)chi.TongChiThang;
                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
            }
        }
        protected void rptThongKeTheoNam_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ChiTheoNam chi = (ChiTheoNam)e.Item.DataItem;
                Label lblSTT = (Label)e.Item.FindControl("lblSTT");
                TongChi += (decimal)chi.TongTien;
                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
            }
        }
        protected void btnThongKe_Click(object sender, EventArgs e)
        {
            if (int.Parse(ddlThongKe.SelectedValue) ==(int)enumThongKe.TheoNgay)
            {
                Q_BUSChi BUSchi = new Q_BUSChi();
                string[] arrValue = dp1.Value.Split('-');
                List<Chi> dsChiNgay = BUSchi.DanhSachChiTheoNgay(new DateTime(int.Parse(arrValue[2]), int.Parse(arrValue[0]), int.Parse(arrValue[1])));
                if (dsChiNgay == null)
                    dsChiNgay = new List<Chi>();
                rptThongKeTheoNgay.DataSource = dsChiNgay;
                rptThongKeTheoNgay.DataBind();
                lblTitle.Text = "Ngày " + dp1.Value;
 
            }
            else if (int.Parse(ddlThongKe.SelectedValue) == (int)enumThongKe.TheoThang)
            {
                Q_BUSChi BUSchi = new Q_BUSChi();
                string[] arrValue = txtChonThang.Value.Split('-');
                List<ChiTheoThang> dsChiThang = BUSchi.dsChiTheoThang(int.Parse(arrValue[0]), int.Parse(arrValue[1]));
                if (dsChiThang == null)
                    dsChiThang = new List<ChiTheoThang>();
                rptThongKeTheoThang.DataSource = dsChiThang;
                rptThongKeTheoThang.DataBind();
                lblTitle.Text = "Tháng " + txtChonThang.Value;
            }
            else
            {
                Q_BUSChi BUSchi = new Q_BUSChi();
                int nam = int.Parse(ddlChonNam.Text);
                rptThongKeTheoNam.DataSource = BUSchi.dsChiTheoNam(nam);
                rptThongKeTheoNam.DataBind();
                lblTitle.Text = "Năm " + ddlChonNam.Text;
            }
        }
    }
}